二叉树的镜像。
解答1[Java]:
核心思想
递归的方式,如果一个节点不等于 null,就交换它的左右子节点,然后对其左右子树进行镜像化。
代码
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public void Mirror(TreeNode root) {
if(root == null){
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
Mirror(root.left);
Mirror(root.right);
}
}